package com.jzr.netty.base;

import com.jzr.common.util.U;
import com.jzr.netty.common.base.DeviceLive;
import io.netty.channel.socket.SocketChannel;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.BeanUtils;

import java.util.ArrayList;
import java.util.List;
import java.util.Map;
import java.util.concurrent.ConcurrentHashMap;

public class DeviceMap {
    private static Logger log = LoggerFactory.getLogger(DeviceMap.class);
    public static Map<String, TDeviceVo> map = new ConcurrentHashMap<>();

    public static  int getSize(){
        return  map.size();
    }

    public static void add(String ip, TDeviceVo record) {
        if(map.entrySet().contains(ip)){
            map.remove(ip);
        }
        map.put(ip, record);
    }

    public static SocketChannel get(String ip) {
        if (map == null || map.size() == 0) {
            return null;
        }
        TDeviceVo tDeviceVo = map.get(ip);
        return tDeviceVo == null ? null : tDeviceVo.getSocketChannel();
    }

    public static int remove(TDeviceVo record) {
        log.info("DeviceMap map.size():" + map.size());
        map.remove(record.getDeviceIp());
        log.info("remove device:" + record.getDeviceIp());
        return 0;
    }


    public static int remove(SocketChannel channel) {
        for (Map.Entry entry : map.entrySet()) {
            TDeviceVo deviceVo = (TDeviceVo)entry.getValue();
            if (deviceVo.getSocketChannel() == channel) {
                map.remove(entry.getKey());
                log.info("remove device:" + entry.getKey().toString());
                return 1;
            }
        }
        return 0;
    }

    public static List<SocketChannel> getSocketChannelList(String deptCode, Integer deviceType){
        List<SocketChannel> list = new ArrayList<>();
        for (Map.Entry entry : map.entrySet()) {
            TDeviceVo deviceVo = (TDeviceVo)entry.getValue();
            if(isCandidated(deviceVo,deptCode,deviceType)){
                list.add(deviceVo.getSocketChannel());
            }
        }
        return list;
    }

    public static List<DeviceLive> getDeviceLive(String deptCode, Integer deviceType){
        List<DeviceLive> list = new ArrayList<>();
        for (Map.Entry entry : map.entrySet()) {
            TDeviceVo deviceVo = (TDeviceVo)entry.getValue();
            if(isCandidated(deviceVo,deptCode,deviceType)){
                DeviceLive tmpDevice = new DeviceLive();
                BeanUtils.copyProperties(deviceVo,tmpDevice);
                list.add(tmpDevice);
            }
        }
        return list;
    }

    public static List<DeviceLive> getDeviceLive(List<String> deviceNoList){
        List<DeviceLive> list = new ArrayList<>();
        if(deviceNoList == null || deviceNoList.isEmpty()){
            return list;
        }
        for (Map.Entry entry : map.entrySet()) {
            TDeviceVo deviceVo = (TDeviceVo)entry.getValue();
            if(deviceNoList.contains(deviceVo.getDeviceNo())) {
                DeviceLive tmpDevice = new DeviceLive();
                BeanUtils.copyProperties(deviceVo, tmpDevice);
                list.add(tmpDevice);
            }
        }
        return list;
    }

    private static boolean isCandidated(TDeviceVo deviceVo, String deptCode, Integer deviceType){
        return (
                (deviceVo != null)
                && (U.isBlank(deptCode) || deviceVo.getDeptCode().equalsIgnoreCase(deptCode))
                && (U.isBlank(deviceType) || deviceVo.getDeviceType().equals(deviceType))
        );
    }


}
